﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability.API.Applications.Command.AquacultureCommand;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.API.Applications.CommandHandler.AquacultureCommandHandler
{
    public class AquacultureQueryCommandHandler : IRequestHandler<AquacultureQueryCommand, APIResult<Aquaculture_Information>>
    {
        private readonly IBaseRepository<Aquaculture_Information> aquacultureRepository;

        public AquacultureQueryCommandHandler(IBaseRepository<Aquaculture_Information> aquacultureRepository)
        {
            this.aquacultureRepository = aquacultureRepository;
        }

        public Task<APIResult<Aquaculture_Information>> Handle(AquacultureQueryCommand request, CancellationToken cancellationToken)
        {
            var res = aquacultureRepository.GetAll().AsNoTracking().FirstOrDefault(s => s.Batch == request.Batch);

            APIResult<Aquaculture_Information> result = new APIResult<Aquaculture_Information>();
            if (res == null)
            {
                result.Code=EnumCode.File;
                result.Msg = "显示失败";
            }
            else
            {
                result.Code = EnumCode.Ok;
                result.Msg = "显示成功";
                result.Data = res;
            }


            return Task.FromResult(result);
        }
    }
}
